<?php
namespace BoxEdu\Controller\Home;
//use Think\Controller;
class IndexController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$this->show_user_info = $this->user_info;

	}

	function git() {
		$command = "cd /wen/www/git/demo2 && git pull";
		$result = exec($command);
		dump($result);
	}

	// function test() {
	// 	BoxModel("edu_knows")->where(1)->save(array('xuebazi' => 4));
	// }
	// function clearTopic() {
	// 	BoxModel("sns_topic")->where(array('pid' => 87))->delete();
	// 	BoxModel("edu_tiy")->where(1)->delete();
	// }

	// function start() {
	// 	S("js_course", null);
	// 	S("tiydata", null);
	// 	$cate_id = "135";
	// 	BoxModel("sns_topic")->where(array('pid' => 87, 'cate_id' => $cate_id))->delete();
	// 	BoxModel("edu_tiy")->where(array('cate_id' => $cate_id))->delete();
	// 	$url = "http://www.w3school.com.cn/css/index.asp";
	// 	$this->test($url);
	// 	$this->getContent();
	// 	$this->tomysql($cate_id);
	// 	$this->to_topic($cate_id);
	// 	exit();
	// }

	// function test($url) {

	// 	header("Content-type: text/html; charset=utf8");
	// 	//采集js

	// 	//$url = "http://www.w3school.com.cn/jquery/index.asp";
	// 	$content = myCurl($url);
	// 	//$content = iconv("GB2312", "UTF-8//IGNORE", $content);
	// 	$phpQuery = new \Common\Org\JAE\QueryList();
	// 	$hobj = \phpQuery::newDocumentHTML($content);
	// 	$robj = pq($hobj)->find("#course")->find("a");
	// 	$course = array();
	// 	foreach ($robj as $key => $value) {
	// 		$url = "http://www.w3school.com.cn/" . pq($value)->attr("href");
	// 		$course[md5($url)] = array(
	// 			'href' => $url,
	// 			'text' => pq($value)->text(),
	// 		);
	// 	}
	// 	S("js_course", $course);
	// 	//dump(S("js_course"));
	// 	//exit();
	// 	//$this->display();
	// }

	// function getContent() {
	// 	$js_course = S("js_course");
	// 	$phpQuery = new \Common\Org\JAE\QueryList();
	// 	//采集js
	// 	foreach ($js_course as $k => $one) {
	// 		$url = $one['href'];
	// 		$content = myCurl($url);
	// 		$content = iconv("GB2312", "UTF-8//IGNORE", $content);
	// 		$hobj = \phpQuery::newDocumentHTML($content);
	// 		$robj = pq($hobj)->find("#maincontent")->html();
	// 		$js_course[$k]['content'] = $robj;
	// 	}
	// 	S("js_course", $js_course);
	// 	//dump(S("js_course"));
	// }

	// function tomysql($cate_id) {
	// 	$js_course = S("js_course");
	// 	$phpQuery = new \Common\Org\JAE\QueryList();
	// 	$i = 0;
	// 	$tiydata = array();
	// 	foreach ($js_course as $k => $one) {
	// 		$hobj = \phpQuery::newDocumentHTML($one['content']);
	// 		$hobj->find("#tpn")->remove();
	// 		$hobj->find("#bpn")->remove();
	// 		$a = $hobj->find("a");
	// 		foreach ($a as $key => $value) {
	// 			$class = pq($value)->text();
	// 			if ($class != "亲自试一试") {
	// 				pq($value)->removeAttr("href");
	// 			} else {
	// 				$href = pq($value)->attr("href");
	// 				$res = $this->getTiy($href, $cate_id);
	// 				$tiydata[$res['name']] = $res;
	// 				$href = "/Group/tiy/name/" . $res['name'];
	// 				pq($value)->attr("href", $href);
	// 			}

	// 		}
	// 		$imgs = $hobj->find("img");
	// 		foreach ($imgs as $key => $value) {
	// 			$src = pq($value)->attr("src");
	// 			//if (strpos($src, "http") != 0) {
	// 			pq($value)->attr("src", "http://www.w3school.com.cn" . $src);
	// 			//}
	// 		}
	// 		$html = $hobj->html();
	// 		$js_course[$k]['content'] = $html;
	// 	}
	// 	S("js_course", $js_course);
	// 	S("tiydata", $tiydata);
	// 	dump(S("js_course"));
	// 	//	exit();

	// }

	// function to_topic($cate_id) {
	// 	$js_course = S("js_course");
	// 	$tiydata = S("tiydata");
	// 	foreach ($tiydata as $key => $value) {
	// 		$row = array(
	// 			'name' => $value['name'],
	// 			'content' => $value['content'],
	// 			'cate_id' => $cate_id,
	// 		);
	// 		$r = BoxModel("edu_tiy")->add($row);
	// 		if ($r) {
	// 			unset($tiydata[$key]);
	// 		}
	// 	}
	// 	$order_by = 1;
	// 	foreach ($js_course as $key => $value) {
	// 		$row = array(
	// 			'content' => $value['content'],
	// 			'title' => $value['text'],
	// 			'user_id' => 101081,
	// 			'pid' => '87',
	// 			'type' => 'eduGroup',
	// 			'cate_id' => $cate_id,
	// 			'dateline' => time(),
	// 			'lastupdate' => time(),
	// 			'order_by' => $order_by,
	// 		);
	// 		$order_by++;
	// 		$r = BoxModel("sns_topic")->add($row);
	// 		if ($r) {
	// 			unset($js_course[$key]);
	// 		}
	// 	}
	// 	S("tiydata", $tiydata);
	// 	S("js_course", $js_course);

	// }

	// function getTiy($url, $cate_id) {
	// 	$data = parse_url($url);
	// 	$name = str_replace("f=", "", $data['query']);
	// 	$phpQuery = new \Common\Org\JAE\QueryList();

	// 	$url = "http://www.w3school.com.cn/" . $url;
	// 	$content = myCurl($url);
	// 	$content = iconv("GB2312", "UTF-8//IGNORE", $content);

	// 	$hobj = \phpQuery::newDocumentHTML($content);
	// 	$content = pq($hobj)->find("#TestCode")->html();
	// 	$r = array(
	// 		'name' => trim($name),
	// 		'content' => trim($content),
	// 		'cate_id' => $cate_id,
	// 	);

	// 	return $r;
	// }

	function test2() {
		// $param = BoxModel("addon_wx_config")->load($this->store_id);
		// $this->wechatObj = new \Common\Org\weiXin($param);
		// $data = array(
		// 	'openid' => 'oPO_fjk873exq75GGO8pNwRUP89Y',
		// 	'name' => '温作权',
		// 	'amount' => '20000', //200分
		// 	'desc' => '测试',
		// );
		// $r = $this->wechatObj->transfersMoney($data);
		// dump($r);
		//发送红包
		// $data = array(
		// 	'openid' => 'oPO_fjk873exq75GGO8pNwRUP89Y',
		// 	'total_amount' => '3000',
		// 	'total_num' => 20,
		// 	'wishing' => '每天学习一小时，坚持！！',
		// 	'act_name' => '大师兄能量传递',
		// 	'remark' => '红包',
		// );
		// $r = $this->wechatObj->sendgroupredpack($data);

	}
	public function index() {
		//dump(md5(C('runThreadKey')));exit();
		$this->icon = array(
			'1' => 'glyphicon-pencil',
			'2' => 'glyphicon-road',
			'3' => 'glyphicon-book',
			'4' => 'glyphicon-star-empty',
		);

		$this->nav_cur = "Index1";
		//dump($this->user_info['subjectids']);exit();
		//dump($this->is_mobile);
		//exit();
		if ($this->is_mobile && (I('box_login_code') || I('code'))) {
			$this->redirect(U('User/index'));
			exit();
		}

		$this->display();
	}

	function subjectList() {

		$this->web_title = "选课中心 - $this->web_title";
		$this->subjectList = BoxModel("edu_subject")->get(array('is_show' => 1), '', "order_by");
		//dump(BoxModel("edu_subject"));
		$this->display();
	}

	public function my_subject() {

		$this->web_title = "我的班级 - $this->web_title";
		$this->nav_cur = "Home";
		if (!$this->user_info['user_id']) {
			$this->redirect("/Index/login");
		}
		$subjectids = (array) json_decode($this->user_info['subjectids'], true);
		if (count($subjectids) == 0 && $this->is_mobile) {
			$this->redirect("/Index/subjectList");
		}
		$this->display();
	}

	function learning() {
		$subjectids = (array) json_decode($this->user_info['subjectids'], true);
		if (count($subjectids) > 0) {
			$result = array_shift($subjectids);
			$user_subjectid = S("user_subjectid_" . $this->user_id);
			$subjectid = $user_subjectid ? $user_subjectid : $result['subjectid'];
			$this->redirect(U('Subject/index', array('subjectid' => $subjectid)));
			exit();
		} else {
			$this->redirect("/Index/my_subject");
		}
	}

	function hot() {
		$this->display();
	}
//打卡
	function getKnowledge() {
		$this->web_title = "打卡学编程-$this->web_title";
		$this->nav_cur = "getKnowledge";
		$this->signData = BoxModel("addon_sign_in")->where(array('user_id' => $this->user_id))->find();
		$setting = json_decode($this->signData['setting'], true);
		//dump($this->signData);

		/******直接进入**/
		$where = 1;
		if ($type = $setting['type']) {
			$where .= " type in ($type)";
		}
		if ($level = $setting['level']) {
			$where .= " level ='$level'";
		}
		/****从课程主页来**/
		if ($type = I('subjectid')) {
			$where = "type='$type'";
		}
		$type = $type ? $type : "all";
		if ($signTypeData = $setting[$type]) {
			$knowledge_id = intval($signTypeData['knowledge_id']);
			$where .= " and id > $knowledge_id";
		}
		//dump($type);
		//dump($signTypeData);
		/******查看***/
		$id = I('id');
		if ($id) {
			$where = array('id' => $id);

		}
		$this->data = BoxModel("addon_knowledge")->where($where)->find();
		if ($id) {
			$signTypeData = $setting[$this->data['type']];
		}
		if ($signTypeData['last_sign_date'] == date("Y-m-d")) {
			$this->has_sign = 1;
			$where = array('id' => $knowledge_id);
		}

		$this->display();
	}
//打卡记录
	function KnowledgeLog() {
		$data = BoxModel("addon_sign_log")->get(array('user_id' => $this->user_id), 10, "id desc");
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	function sign() {
		$this->signData = BoxModel("addon_sign_in")->where(array('user_id' => $this->user_id))->find();
		$knowledge = I('knowledge');
		$knowledgeData = BoxModel("addon_knowledge")->where(array('id' => $knowledge))->find();
		$setting = (array) json_decode($this->signData['setting'], true); //不同类开的打卡时间
		$type = $knowledgeData['type'] ? $knowledgeData['type'] : "all";
		$signTypeData = $setting[$type];
		if (!$this->user_id) {
			exit(json_encode(array('error' => 1, 'msg' => '请先登录')));
		}
		if ($signTypeData['last_sign_date'] == date("Y-m-d")) {
			exit(json_encode(array('error' => 2, 'msg' => '你今天已打卡了')));
		}
		if ($this->signData['last_sign_date'] == date("Y-m-d", strtotime("-1 days"))) {
			$days = $this->signData['days'] + 1;
		} else {
			$days = 1;
		}

		$setting[$type] = array(
			'last_sign_date' => date("Y-m-d"),
			'knowledge_id' => $knowledge,
		);
		$row = array(
			'user_id' => $this->user_id,
			'point' => intval($this->signData['point']) + $days,
			'count' => intval($this->signData['count']) + 1,
			'last_sign_date' => date("Y-m-d"),
			'days' => $days,
			'knowledge' => $knowledge,
			'setting' => json_encode($setting),
		);
		if ($this->signData) {
			$r = BoxModel("addon_sign_in")->where(array('user_id' => $this->user_id))->save($row);
		} else {
			$row['frist_sign_time'] = date("Y-m-d");
			$r = BoxModel("addon_sign_in")->add($row);
		}
		if ($r) {
			//打卡日志
			$row2 = array(
				'days' => $days,
				'title' => $knowledgeData['title'],
				'addtime' => time(),
				'user_id' => $this->user_id,
				'knowledge_id' => $knowledge,
			);
			BoxModel("addon_sign_log")->add($row2);
			//积分
			$row3 = array(
				'event' => '00022',
				'title' => $knowledgeData['title'],
				'num' => $days,
				'days' => $days,
				'url' => HomeUrl . U('Index/getKnowledge', array('id' => $knowledge)),
				'subjectid' => $knowledgeData['type'],
			);
			Logic("BoxEdu", "User")->add_xuebazi($row3);
			exit(json_encode(array('error' => 0, 'msg' => $row)));
		} else {
			exit(json_encode(array('error' => 3, 'msg' => '系统错误')));
		}

	}

	function earn() {
		if (!$this->user_info['user_id']) {
			$this->jump("", "/Index/login");
		}
		$this->display();
	}

	public function class_info() {
		$this->class_id = I('class_id');
		$this->class_data = BoxModel("edu_subject_class")->where(array('id' => $this->class_id))->find();

	}

	public function subject() {

		$this->class_info();
		$this->class_nav = "subject";
		$this->nav_cur = "selectSubject";
		$this->web_title = $this->class_data['name'] . "学院 - $this->web_title";
		$subject_list = BoxModel("edu_subject")->get(array('subject_class' => $this->class_id, 'is_show' => 1), "50", "order_by");
		$this->subject_list = $subject_list['list'];
		//  dump( $this->subject_list);
		$this->display();
	}

	public function subject_detail() {

		$this->subjectid = $subjectid = I('subject_id');
		$this->nav_cur = "selectSubject";
		$this->data = BoxModel("EduSubject")->load($subjectid);
		///	dump($this->data);
		/*******线下课程**/
		$school_id = $this->user_info['school_id'];
		$param = array(
			'school_id' => $school_id,
			'subjectid' => $subjectid,
		);
		$this->web_title = $this->data['subject'] . " - $this->web_title";
		$this->offline_subject = Logic("BoxEdu", "Subject")->get_offline_subject_price($param);
		$this->buy_count = BoxModel("edu_subject_order")->where(array('subjectid' => $subjectid, 'status' => array('in', "2,4")))->count();
		$this->display();
	}

	public function down() {

		$id = I('id');
		$this->nav_cur = "selectSubject";
		$model = BoxModel("edu_attach");
		$data = $model->where(array('attid' => $id))->find();
		$data['attext'] = stripslashes(htmlspecialchars_decode($data['attext']));
		$data['active_text'] = stripslashes(htmlspecialchars_decode($data['active_text']));
		$this->data = $data;
		$this->web_title = $this->data['title'] . "下载 - $this->web_title";
		$this->display();
	}

	//下载获得积分
	public function down_user() {
		ignore_user_abort(true);
		$id = I('id');
		$data = BoxModel("edu_attach")->where(array('attid' => $id))->find();
		$where = array(
			'attid' => $id,
			'user_id' => $this->user_info['user_id'],
		);
		$r = BoxModel("edu_attach_user")->where($where)->find();
		if (!$r) {
			$bool = BoxModel("edu_attach_user")->add($where);
			if ($bool) {
//下载获得积分
				$row = array(
					'num' => $data['xuebazi'],
					'subjectid' => $data['class_id'],
					'title' => $data['atttitle'],
					'url' => HomeUrl . U('Index/down', array('id' => $data['attid'])),
					'event' => $data['type'] == 1 ? "00021" : "00002",
				);
				Logic("BoxEdu", "User")->add_xuebazi($row);
			}
		}
	}

	public function registerForExamination() {
		$this->class_id = $class_id = I('class_id');
		$this->subjectid = $subjectid = I('subjectid');
		$this->class_info = BoxModel("edu_subject_class")->where(array('id' => $class_id))->find();
		$where = array(
			'user_id' => $this->user_id,
			'classid' => $class_id,
		);
		$model = BoxModel('edu_apply_examination');
		$r = $model->where($where)->find();
		$id = $r['id'];
		$apply = array();
		if ($id) {
			$apply = $model->load($id);
		}
		$user_info = $this->user_info;
		unset($user_info['id']);
		unset($user_info['status']);
		unset($user_info['remark']);
		$apply = array_merge($user_info, $apply);
		//		dump($apply);
		$this->apply = $apply;
		$this->display();
	}

	public function doRegisterForExamination() {
		$class_id = I('class_id');
		$subjectid = I('subjectid');
		$class_info = BoxModel("edu_subject_class")->where(array('id' => $class_id))->find();
		$subjec_info = BoxModel("edu_subject")->where(array('subjectid' => $subjectid))->find();
		$model = BoxModel('edu_apply_examination');
		$param = $model->create();
		$data = array_diff(I(), $param);
		$data['class_name'] = $class_info['name'];
		$data['subject_name'] = $subjec_info['subject'];
		//dump($class_info);exit();
		$examination_id = I('id');
		$info = $this->_upload();
		if (is_array($info)) {
			foreach ($info as $k => $one) {
				$data[$k] = array(
					'file_url' => $one['savepath'] . $one['savename'],
					'file_name' => $one['name'],
				);
			}
		}
		$row = array(
			'classid' => $class_id,
			'subjectid' => $subjectid,
			'user_id' => $this->user_id,
			'money' => $subjec_info['examination_fee'],
			'add_time' => date("Y-m-d H:i:s"),
			'status' => 1,
			'data' => serialize($data),
		);
		$model->startTrans();
		if ($examination_id) {
			$r = $model->where(array('id' => $examination_id))->save($row);
		} else {
			$examination_id = $model->add($row);
		}
		if ($examination_id) {
			exit(json_encode(array('error' => 0, 'msg' => $examination_id)));
		} else {
			exit(json_encode(array('error' => 1, 'msg' => "报考信息提交失败")));
		}

		// //生成订单
		//        if($examination_id){
		//           // $HomeUrl = BOX_MODULE_NAME == "Wap" ? MEduUrl : EduUrl;
		//            $suceess_url = HomeUrl . U('User/apply_examination');
		//            $row = array(
		//                'store_id' => 1,
		//                'user_id' => $this->user_id,
		//                'payment_id' => intval(I('pay_type')),
		//                'order_amount' => $class_info['fee'],
		//                'goods_amount' =>$class_info['fee'],
		//                'order_type' => 'examination',
		//                'suceess_url' => $suceess_url,
		//                'examination_id'=>$examination_id,
		//                'type'          =>'examination',
		//                'title' => '报考名称：' . $class_info['name'],
		//                'body' => '报考名称：' . $class_info['name'] ,
		//            );
		//            $orderData = Logic("BoxMall", "Order")->add($row);
		//
		//        }
		//        if($r && $orderData){
		//            $model->commit();
		//            $order_sn=$orderData['order_sn'];
		//            if ($row['order_amount'] > 0) {//在线结算金额如果大于0
		//                echo json_encode($this->return_array(0, HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html"));
		//            } else {
		//                session("pay_verify", true);
		//                A("BoxPay/Order", "", 1)->succeed($order_sn);
		//                if ($r) {
		//                    echo json_encode($this->return_array(0, $suceess_url));
		//                } else {
		//                    echo json_encode($this->return_array(1, "订单提交失败"));
		//                }
		//            }
		//
		//        }else{
		//            $model->rollback();
		//            $this->jump("提交失败");
		//        }
	}

	public function course() {
		$this->class_info();
		$this->class_nav = "course";
		$this->subject_list = BoxModel("edu_subject")->get(array('S.subject_class' => $this->class_id));
		$this->nav_cur = "selectSubject";
		//dump($this->subject_comment);
		$this->display();
	}

	public function rank() {
		$this->class_nav = "rank";
		$this->class_info();
		$this->display();
	}

	public function voice() {
		$this->class_nav = "voice";
		$this->class_info();
		$this->subject_comment = BoxModel("edu_subject_order")->getComment(array('class_id' => $this->class_id), 5);
		$this->show_page($this->subject_comment['page']);
		$this->display();
	}

	/*****校园动态**/
	public function timeline() {
		$this->timeListWhere = "1";
		$this->tab = $type = I('type', "tab1");
		if ($user_id = I('user_id')) {
			$this->show_user_info = Logic("BoxEdu", "User")->info($user_id);
			$this->to_user_id = $user_id;
			$class_ids = $this->show_user_info['classids'];
		}
		if ($this->to_user_id == $this->user_info['user_id']) {
			$this->nav_cur = "User";
		} else {
			/**访友*/
			Logic("BoxEdu", "User")->visit($this->to_user_id);
		}
		/*******学员动态***/
		if ($type == "tab1") {
			$this->parm .= $user_id;
			$this->str_timeline = "timeline";
		}
		/*******班级动态***/
		if ($type == "tab2") {
			$this->parm = $class_ids;
			$this->str_timeline = "class_timeline";
		}
		/*******访友***/
		if ($type == "tab3") {
			$this->parm = $user_id;
			$this->str_timeline = "user_visit";
		}
		$this->display();
	}

	/******班级动态展示***/
	public function class_new_info() {
		$id = I('id');
		$this->data = BoxModel("SnsTopic")->load($id);
		//  dump($this->data);
		$this->display();
	}

	/*****学员心声**/
	public function xueyuan() {
		$this->title = "学员心声";
		if ($this->subjectid = I('subjectid')) {
			$this->where = "subjectid='$this->subjectid'";
			$data = BoxModel("EduSubject")->load($this->subjectid);
			$this->title = $data['subject'];
		}
		$data = BoxModel("SnsComment")->get("subject_order", $this->where, 10, "comment_id desc");
		$this->data = $data['list'];
		$this->show_page($data['pager']);
		$this->display();
	}

	/*******登录***/
	public function login() {
		// if ($this->user_info['user_id']) {
		//            $this->jump("", "/");
		//        }
		$this->display("Index/login");
	}

	/*********发送手机验证码****/
	public function send_code() {
		$code = rand(100000, 999999);
		$tel = I('tel');
		if (!preg_match('/^1\d{10}$/', $tel)) {
			exit(json_encode(array(
				'error' => 1,
				'msg' => '手机号必须是1开头的11位数字',
			)));
		}
		/*******发送短信******/
		$r = sms($tel, "验证码：" . $code . "【盛明体育科技】");
		session("tel_code", $code);
		exit(json_encode(array(
			'error' => 0,
			'msg' => $code,
		)));
		if ($r === true) {
			session("tel_code", $code);
			exit(json_encode(array(
				'error' => 0,
				'msg' => "验证码已经发送成功，请注意查收",
			)));
		} else {
			exit(json_encode(array(
				'error' => 1,
				'msg' => $r,
			)));
		}
	}

	public function content() {
		$topic_id = I('topic_id', "0", "intval");
		$this->data = BoxModel("sns_topic")->where(array('id' => $topic_id))->find();
		$this->web_title = $this->data['title'] . " - $this->web_title";
		$this->display();
	}
}
